<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}
include 'topmenu.php';

$avialable_langs = explode(",", $option['avialable_langs']);
$avialable_langs_desc = explode(",", $option['avialable_langs_desc']);

$showForm=1;

if (!isset($FormError))
{
	$i=0;
	$FormError=array();
}

if (isset($act) && ($act==1 || $act==2))
{
	if ($act==1){$ActionUrl=1;  $ActionButton=_ADDNEW_; $queryTitleCheck="SELECT titles_mkey FROM ".$PortalPref.$amk." WHERE titles_mkey='".$mkey."'";}
	if ($act==2){$ActionUrl=2;  $ActionButton=_EDITE_; $midActionUrl='&tid='.$tid; $queryTitleCheck="SELECT titles_mkey FROM ".$PortalPref.$amk." WHERE titles_id='".$tid."'";}

	if ($act==1 || $act==2)
	{
		$queryTitleCheck=mysql_query($queryTitleCheck);
		if (mysql_num_rows($queryTitleCheck)!=0 && $act==1)
		{
			$FormError[$i]=_TITLE_EXIST_;
			$i++;
		}elseif(mysql_num_rows($queryTitleCheck)==0 && $act==2)
		{
			$FormError[$i]=_NO_TITLE_FOR_EDITE_;
			$i++;
			$showForm=0;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
		}else
		{
			if ($act==2 && (!isset($tid) || !is_numeric($tid)))
			{
				$FormError[$i]=_NO_TITLE_FOR_EDITE_;
				$i++;
				$showForm=0;
				echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
			}

			if (!isset($mkey) || $mkey=='' || is_numeric($mkey))
			{
				$FormError[$i]=_NO_MODULE_KEY_;
				$i++;
			}

			$tMaskCheck=false;
			if (!isset($tmask) || $tmask=='')
				$tMaskCheck=true;

            if (!isset($tgenerate) || empty($tgenerate) || $tgenerate=='' || !is_numeric($tgenerate))
            {
				$ValueCheckCount=0; $lValueError=array(); $lVi=0;
				for($lid=0; $lid<count($avialable_langs); $lid++)
				{
					$lvalueCheck='lvalue_'.$avialable_langs[$lid];

					if ($_POST[$lvalueCheck]!='')
					{
						$ValueCheckCount++;
					}else
					{						$lValueError[$lVi]=_NO_TITLE_KYEWORDS_.' <b>'._FOR_.'</b> '._LANGA_.': '.$avialable_langs_desc[$lid].'<b>!</b>';
						$lVi++;
					}
				}

        	    if ($ValueCheckCount!=0)
            	{            		if($ValueCheckCount!=$lid)
	            	{    	        		for($lVi=0; $lVi<count($lValueError); $lVi++)
        	    		{            				$FormError[$i]=$lValueError[$lVi];
							$i++;            			}
	            	}
    	        }else
        	    {            		if ($tMaskCheck)
            		{            			$FormError[$i]=_ONE_TITLE_FIELD_REQUERED_;
						$i++;
    	        	}
        	    }
        	 }

			if (empty($FormError))
			{
				if (!isset($tgenerate) || empty($tgenerate) || $tgenerate=='' || !is_numeric($tgenerate))
					$t_generate=0;
				else
					$t_generate=1;

				if ($act==1)
				{
					if ($t_generate==0 && $ValueCheckCount!=0)
						$tKewordsConstant=SetTitleForLang($mkey);					else
	                	$tKewordsConstant='';
					$queryTitle="INSERT INTO ".$PortalPref.$amk."
						(
							titles_id,
							titles_mkey,
							titles_mask,
							titles_keywords,
							titles_key_generate
						)VALUES
						(
							NULL,
							'".$mkey."',
							'".$tmask."',
							'".$tKewordsConstant."',
							'".$t_generate."'
						)";

				}elseif($act==2)
				{
					$queryTitleAddKeywords='';
					if ($t_generate==1)
					{
						$queryTitleAddKeywords=" titles_keywords='', ";
					}else
					{						if($ValueCheckCount!=0)
						{
							if($t_title=='')
							{
								$tKewordsConstant=SetTitleForLang($mkey);
								$queryTitleAddKeywords=" titles_keywords='".$tKewordsConstant."', ";
							}else
								$queryTitleAddKeywords='';
						}else
							$queryTitleAddKeywords=" titles_keywords='', ";					}

					$queryTitle="UPDATE ".$PortalPref.$amk."
					SET
						titles_mask='".$tmask."',
						".$queryTitleAddKeywords."
						titles_key_generate='".$t_generate."'
					WHERE
						titles_id='".$tid."'
					";

					$nextStep=true;
				}
                //print $queryTitle;
				if (mysql_query($queryTitle))
				{
					if ($act==1 && $t_generate==0)
					{
                    	if ($ValueCheckCount!=0)
                    	{
	                    	for ($li=0; $li<count($avialable_langs); $li++)
							{
								$LValue='lvalue_'.$avialable_langs[$li];
								$value=trim($$LValue);

								mysql_query("INSERT INTO ".$PortalPref."langs_".$avialable_langs[$li]."
								(
									lang_id,
									lang_module,
									lang_param,
									lang_value
								)VALUES
								(
									NULL,
									'portal',
									'".$tKewordsConstant."',
									'".$value."'
								)");
							}
						}
					}
					$nextStep=true;

					if($act==2)
					{						if ($t_generate==0)
						{							if (isset($t_title) && $t_title!='' && !empty($t_title))
							{
								if($ValueCheckCount==0)
								{									for($i=0; $i<count($avialable_langs); $i++)
									{
										mysql_query("DELETE FROM ".$PortalPref."langs_".$avialable_langs[$i]." WHERE lang_param='".$t_title."'");
									}
								}else
								{
									for($i=0; $i<count($avialable_langs); $i++)
									{
										$LValue='lvalue_'.$avialable_langs[$i];
										$value=trim($$LValue);
										mysql_query("UPDATE ".$PortalPref."langs_".$avialable_langs[$i]." SET lang_value='".$value."' WHERE lang_param='".$t_title."'");
									}
								}
							}else
							{								if($ValueCheckCount!=0)
								{									for ($li=0; $li<count($avialable_langs); $li++)
									{
										$LValue='lvalue_'.$avialable_langs[$li];
										$value=trim($$LValue);
										mysql_query("INSERT INTO ".$PortalPref."langs_".$avialable_langs[$li]."
										(
											lang_id,
											lang_module,
											lang_param,
											lang_value
										)VALUES
										(
											NULL,
											'portal',
											'".$tKewordsConstant."',
											'".$value."'
										)");
									}								}else
								{									print "5";
								}
							}
						}else
						{      						if (isset($t_title) && $t_title!='' && !empty($t_title))
      						{
      							for($i=0; $i<count($avialable_langs); $i++)
								{
									mysql_query("DELETE FROM ".$PortalPref."langs_".$avialable_langs[$i]." WHERE lang_param='".$t_title."'");
								}
							}						}
					}

					if($nextStep)
					{
						$showForm=0;
						print '<p align="center">'._OPERATION_SUCCESS_.'</p>';
						echo"<meta http-equiv='refresh' content='0;url=/admin.php?amk=".$amk."'>";
					}
					//exit;
				}else
				{
					$FormError[$i]=_BAD_SQL_;
					$i++;
				}
			}
		}
	}
}elseif(isset($act) && $act==3)
{
	$ActionUrl=2;  $ActionButton=_EDITE_;
	$queryTitleSelect=mysql_query("SELECT * FROM ".$PortalPref.$amk." WHERE titles_id='".$tid."'");
	if (mysql_num_rows($queryTitleSelect)!=1)
	{
		$FormError[$i]=_NO_TITLE_FOR_EDITE_;
		$i++;
		$showForm=0;
		echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";

	}else
	{
		while($resultTitleSelect=mysql_fetch_assoc($queryTitleSelect))
		{
			$mkey=$resultTitleSelect['titles_mkey'];
			$tmask=$resultTitleSelect['titles_mask'];
			$tgenerate=$resultTitleSelect['titles_key_generate'];
			$lvid=$resultTitleSelect['titles_keywords'];
			$t_title=$lvid;
		}
		$midActionUrl='&tid='.$tid;

		if ($tgenerate==0 && $lvid!='')
		{
			$count=count($avialable_langs);
			for($li=0; $li<$count; $li++)
			{
				$available_langs1[$li]=$avialable_langs[$li].".lang_value AS lvalue_".$avialable_langs[$li];
				$available_langs2[$li]=$PortalPref."langs_".$avialable_langs[$li]." ".$avialable_langs[$li];
				$available_langs3[$li]=$avialable_langs[$li].".lang_param='".$lvid."'";
			}

			$valueArray = implode(",", $available_langs1);
			$tableArray= implode(",", $available_langs2);
			$WhereArray= implode(" AND ", $available_langs3);

			$queryLangValueSelect=mysql_query("SELECT ".$option['default_lang'].".lang_module, ".$valueArray." FROM ".$tableArray." WHERE ".$WhereArray." AND ".$option['default_lang'].".lang_module='portal'");

			if (mysql_num_rows($queryLangValueSelect)!=1)
			{
				$FormError[$i]=_NO_KEYWORDS_TITLE_FOR_EDITE_;
				$i++;
				$showForm=0;
				echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToRef."'>";
			}else
			{
				while($resultLangValueSelect=mysql_fetch_assoc($queryLangValueSelect))
				{
					extract($resultLangValueSelect);
					$lparam=$lvid;
				}
			}
		}
	}
}else
{
	$ActionUrl=1;  $ActionButton=_ADDNEW_;
}

print '<p>';
getErrorList($FormError);
print '</p>';

if (isset($tgenerate) && $tgenerate==1)
	$tgenerate_flag='checked';
else
	$tgenerate_flag='';

if ($showForm==1)
{
	if (!isset($mkey) || empty($mkey) || $mkey=='' || is_numeric($mkey))
	{
		$moduleSelected='-1';
	}else
		$moduleSelected=$mkey;

	print '<form method="post" name="ModuleFrm" action="'.$portal_url.'/admin.php?amk='.$amk.'&apk='.$apk.'&act='.$ActionUrl.''.$midActionUrl.'">
		<table width="80%" align="center">';
			if ($ActionUrl==1)
			{
				print'<tr>
					<td>'._MODULE_.'</td>
					<td>';
						GetModuleList($moduleSelected,0,'mkey','', 'user', 1);
					print'</td>
				</tr>';
			}else
				print '<input name="mkey" type="hidden" value="'.$mkey.'">';
			print'<tr>
				<td>'._TITLES_MASKS_.'</td>
				<td>
					<input type="button" value="[P]" id="p" onclick="setText(this, \'tmask\');">
					<input type="button" value="[M]" id="m" onclick="setText(this, \'tmask\');">
					<input type="button" value="[D]" id="d" onclick="setText(this, \'tmask\');">
					<input type="button" value="[T]" id="t" onclick="setText(this, \'tmask\');">
					<input type="button" value="[G]" id="u" onclick="setText(this, \'tmask\');">
					<input type="text" name="tmask" id="tmask" size="15" value="'.$tmask.'">
				</td>
			</tr>
			<tr>
				<td>'._TITLES_KEWORDS_GENERATE_.'</td>
				<td><input name="tgenerate" type="checkbox" value="1" '.$tgenerate_flag.'></td>
			</tr>
			<tr>
				<td>'._TITLES_USER_KEYWORDS_.'</td>
				<td>';
					print GetLangsSelector($avialable_langs, $avialable_langs_desc);
					for ($li=0; $li<count($avialable_langs); $li++)
					{
						$valueVarName='lvalue_'.$avialable_langs[$li];
                        if ($li>0)
							$classAdd='hidden';
						else
							$classAdd='';
						print '<div class="tabLangEdite '.$classAdd.'" id="area_'.$avialable_langs[$li].'"><textarea name="lvalue_'.$avialable_langs[$li].'" rows=5 cols=20>'.$$valueVarName.'</textarea></div>';
					}
					if ($ActionUrl==2)
						print '<input type="hidden" name="t_title" value="'.$t_title.'">';
				print'</td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="'.$ActionButton.'"></td>
			</tr>
		</table>
	</form>';
}
?>